using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TestGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        SpriteManager sprites = new SpriteManager();
        static TileMapManager tiles;
        public static short TILE_WIDTH = 32;
        public static short TILE_HEIGHT = 32;
        public static float GLOBAL_SIZE = 0.5f;
        public static Player player { private set; get; }
        public static List<HUD> hud = new List<HUD>();

        Map _curmap;
        Map CurrentMap
        {
            get { return _curmap; }
            set
            {
                _curmap = value;
                camera.Limits = new Rectangle(0, 0, value.totalWidth, value.totalHeight);
            }
        }

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public static Camera camera;

        public Game()
        {
            TILE_HEIGHT = (short)(TILE_HEIGHT * GLOBAL_SIZE);
            TILE_WIDTH = (short)(TILE_WIDTH * GLOBAL_SIZE);
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            SpriteManager.manager = this.Content;
            IsMouseVisible = true;
            camera = new Camera(graphics.GraphicsDevice.Viewport, 1f);
            base.Initialize();

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            tiles = new TileMapManager();

            // a bunch of test values, except for the player
            CurrentMap = new Map(TileMapManager.maps["Test Map"]);
            new Tower(64, 64, "sprites/tower");
            new Tower(256, 256, "sprites/tower");
            player = new Player(0, 0, GLOBAL_SIZE, "sprites/WizardSquare");
            new HUD(96, Window.ClientBounds.Height - 32, "sprites/sprite");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // spritemanager updates everything
            sprites.Update(gameTime, camera, CurrentMap);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            //drawing sprites
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, camera.GetViewMatrix(Vector2.One));

            sprites.Draw(spriteBatch, graphics.GraphicsDevice);

            spriteBatch.End();

            //drawing HUD
            spriteBatch.Begin();

            foreach (HUD h in hud)
            {
                h.Draw(spriteBatch);
            }

            //draw mouse cursor
            if (Game.player.mouseCursor != null)
            {
                MouseState mouseState = Mouse.GetState();

                IsMouseVisible = false;

                spriteBatch.Draw(Game.player.mouseCursor, new Vector2(mouseState.X, mouseState.Y), Color.White);
            }
            else
            {
                IsMouseVisible = true;
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
